package com.ton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ton.dao.BookDao;
import com.ton.domain.BookBean;
import com.ton.service.BookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookDao bookDao;

    /**
     * 保存图书
     *
     * @param bookBean
     * @return
     */
    @Override
    public boolean save(BookBean bookBean) {


        return bookDao.saveBook(bookBean) > 0;
    }

    /**
     * 修改图书
     *
     * @param bookBean
     * @return
     */
    @Override
    public boolean update(BookBean bookBean) {
        return bookDao.updateById(bookBean) > 0;
    }

    /**
     * 根据id查询图书
     *
     * @param id
     * @return
     */
    @Override
    public BookBean getById(Integer id) {
        return bookDao.selectById(id);
    }

    /**
     * 获得全部图书
     *
     * @return
     */
    @Override
    public List<BookBean> selectAll() {

        return bookDao.selectList(null);
    }

    /**
     * 分页获取图书
     *
     * @param currentPage 当前页码
     * @param pageSize    单页条数
     * @return
     */
    @Override
    public IPage<BookBean> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage, pageSize);
        bookDao.selectPage(page, null);
        return page;
    }

    @Override
    public boolean deleteList(List<BookBean> data) {

        return bookDao.deleteBatchIds(data) > 0;
    }

    /**
     * 分页查询图书
     *
     * @param currentPage 当前页码
     * @param pageSize    单页条数
     * @param bookBean        实体类对象
     * @return
     */
    @Override
    public IPage<BookBean> getPage(int currentPage, int pageSize, BookBean bookBean) {
        IPage page = new Page(currentPage, pageSize);
        LambdaQueryWrapper<BookBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(bookBean.getAuthor()), BookBean::getAuthor, bookBean.getAuthor());
        queryWrapper.like(Strings.isNotEmpty(bookBean.getTitle()), BookBean::getTitle, bookBean.getTitle());
        queryWrapper.like(Strings.isNotEmpty(bookBean.getPress()), BookBean::getPress, bookBean.getPress());
        bookDao.selectPage(page, queryWrapper);
        return page;
    }
}
